package com.hxy.es.service;


import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @description: ES索引操作
 * @program: springbootTest
 * @author: HCY
 * @create: 2022/03/24 15:14
 */
@Service
public class EsIndexService {


    @Value("${esParam.IndexName}")
    private String IndexName;
    @Value("${esParam.indexType}")
    private String indexType;

    @Autowired
    RestHighLevelClient esClient;

    /**
     * 创建索引
     * @return true -> 成功 false ->失败
     */
    public boolean addIndex(String indexName)  {
        //1.使用client获取操作索引对象
        IndicesClient indices = esClient.indices();
        //2.具体操作获取返回值
        //2.1 设置索引名称
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        CreateIndexResponse createIndexResponse = null;
        try {
            createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        //3.根据返回值判断结果
        return createIndexResponse.isAcknowledged();
    }

    /**
     * 创建索引且添加配置
     * @throws IOException
     */
    public boolean addIndexAndSetting(String indexName)  throws IOException {
        IndicesClient indices = esClient.indices();
        //2.具体操作，获取返回值
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        //2.1 设置settings
        String settings = "{\"index.analysis.analyzer.default.type\":\"ik_max_word\"}";
        // 可以单独设置mapping
        String mapping = "{\n" +
                "      \"properties\" : {\n" +
                "        \"address\" : {\n" +
                "          \"type\" : \"text\",\n" +
                "          \"analyzer\" : \"ik_max_word\"\n" +
                "        },\n" +
                "        \"age\" : {\n" +
                "          \"type\" : \"long\"\n" +
                "        },\n" +
                "        \"name\" : {\n" +
                "          \"type\" : \"keyword\"\n" +
                "        }\n" +
                "      }\n" +
                "    }";
        // createIndexRequest.mapping(settings, XContentType.JSON);
        createIndexRequest.settings(settings,XContentType.JSON);

        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        //3.根据返回值判断结果
       return createIndexResponse.isAcknowledged();
    }

    /**
     * 查询索引的信息
     * @param indexName
     * @throws IOException
     */
    public GetIndexResponse queryIndex(String indexName) throws IOException {
        IndicesClient indices = esClient.indices();
        GetIndexRequest getRequest=new GetIndexRequest(indexName);
        GetIndexResponse response = indices.get(getRequest, RequestOptions.DEFAULT);
        return response;
    }

    /**
     * 删除索引
     * @throws IOException
     */
    public boolean deleteIndex(String indexName) throws IOException {
        IndicesClient indices = esClient.indices();
        DeleteIndexRequest deleteRequest=new DeleteIndexRequest(indexName);
        AcknowledgedResponse delete = indices.delete(deleteRequest, RequestOptions.DEFAULT);
        return delete.isAcknowledged();
    }

    /**
     * 索引是否存在
     * @param indexName
     * @return
     * @throws IOException
     */
    public boolean existIndex(String indexName) throws IOException {
        IndicesClient indices = esClient.indices();
        GetIndexRequest getIndexRequest=new GetIndexRequest(indexName);
        boolean exists = indices.exists(getIndexRequest, RequestOptions.DEFAULT);
        return exists;
    }







}
